package com.note.app.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.note.common.domain.CollaborationOperation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 协作操作Mapper接口
 */
@Mapper
public interface CollaborationOperationMapper extends BaseMapper<CollaborationOperation> {
    
    /**
     * 查询会话的操作历史
     *
     * @param sessionId 会话ID
     * @param limit 限制数量
     * @return 操作历史
     */
    @Select("SELECT * FROM collaboration_operation WHERE session_id = #{sessionId} ORDER BY version DESC LIMIT #{limit}")
    List<CollaborationOperation> selectHistoryBySessionId(@Param("sessionId") Long sessionId, @Param("limit") int limit);
    
    /**
     * 查询会话最新版本号
     *
     * @param sessionId 会话ID
     * @return 最新版本号
     */
    @Select("SELECT MAX(version) FROM collaboration_operation WHERE session_id = #{sessionId}")
    Integer selectMaxVersionBySessionId(@Param("sessionId") Long sessionId);
    
    /**
     * 查询会话特定版本的操作
     *
     * @param sessionId 会话ID
     * @param version 版本号
     * @return 操作记录
     */
    @Select("SELECT * FROM collaboration_operation WHERE session_id = #{sessionId} AND version = #{version} LIMIT 1")
    CollaborationOperation selectBySessionIdAndVersion(@Param("sessionId") Long sessionId, @Param("version") Integer version);
    
    /**
     * 统计会话的操作数量
     *
     * @param sessionId 会话ID
     * @return 操作数量
     */
    @Select("SELECT COUNT(*) FROM collaboration_operation WHERE session_id = #{sessionId}")
    Integer countBySessionId(@Param("sessionId") Long sessionId);
    
    /**
     * 查询会话的第一个操作
     *
     * @param sessionId 会话ID
     * @return 第一个操作
     */
    @Select("SELECT * FROM collaboration_operation WHERE session_id = #{sessionId} ORDER BY version ASC LIMIT 1")
    CollaborationOperation selectFirstBySessionId(@Param("sessionId") Long sessionId);
    
    /**
     * 查询会话的最后一个操作
     *
     * @param sessionId 会话ID
     * @return 最后一个操作
     */
    @Select("SELECT * FROM collaboration_operation WHERE session_id = #{sessionId} ORDER BY version DESC LIMIT 1")
    CollaborationOperation selectLastBySessionId(@Param("sessionId") Long sessionId);
} 